package cms.manage;

import cms.bean.setting.SystemSetting;
import cms.bean.user.AccessUser;
import cms.utils.SpringContextHolder;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 验证码管理
 */
@Component("captchaManage")
public class CaptchaManage {


    /**---------------------------------- 验证码 ---------------------------------------**/
    /**
     * 生成验证码
     *
     * @param captchaKey 验证码KEY
     * @param captcha    验证码
     * @return
     */
    @Cacheable(value = "captchaManage_cache_captcha", key = "#captchaKey")
    public String captchaGenerate(String captchaKey, String captcha) {
        return captcha;
    }

    /**
     * 删除验证码
     *
     * @param captchaKey 验证码KEY
     * @return
     */
    @CacheEvict(value = "captchaManage_cache_captcha", key = "#captchaKey")
    public void captchaDelete(String captchaKey) {
    }

    /**---------------------------------- 令牌 ---------------------------------------**/

    /**
     *  生成员工令牌标记
     * @param token 令牌标记
     * @param sessionId
     * @return

     @Cacheable(value="captchaManage_cache_token",key="#token") public String token_generate(String token,String sessionId){
     return sessionId;
     }*/
    /**
     *  删除员工令牌标记
     * @param captchaKey 验证码KEY
     * @return

     @CacheEvict(value="captchaManage_cache_token",key="#token") public void token_delete(String token){
     }*/

    /**---------------------------------- 话题 ---------------------------------------**/
    /**
     * 是否显示验证码
     *
     * @param accessUser 用户名称
     * @param act
     * @return 验证码标记
     */
    public boolean actIsCaptcha(AccessUser accessUser, String act) {
        boolean isCaptcha = false;
        SettingManage settingManage = SpringContextHolder.getBean(SettingManage.class);
        SystemSetting systemSetting = settingManage.findSystemSettingCache();
        int submitQuantity = systemSetting.getSubmitQuantity(act);
        if (submitQuantity <= 0) {//为0时每次都出现验证码
            isCaptcha = true;
        } else if (accessUser != null) {
            //用户每分钟提交次数
            Integer quantity = settingManage.getSubmitQuantity(act, accessUser.getUserName());
            //如果每用户每分钟提交超过设定次数，则需验证码
            if (quantity != null && quantity >= submitQuantity) {
                isCaptcha = true;
            }
        }
        return isCaptcha;
    }


    /**---------------------------------- 评论 ---------------------------------------**/
    /**
     * 是否显示验证码
     *
     * @param accessUser 用户名称
     * @return 验证码标记
     */
    public boolean commentIsCaptcha(AccessUser accessUser) {
        return actIsCaptcha(accessUser, "comment");
    }


    /**---------------------------------- 问题 ---------------------------------------**/
    /**
     * 是否显示验证码
     *
     * @param accessUser 用户名称
     * @return 验证码标记
     */
    public boolean questionIsCaptcha(AccessUser accessUser) {
        return actIsCaptcha(accessUser, "question");
    }


    /**---------------------------------- 答案 ---------------------------------------**/
    /**
     * 是否显示验证码
     *
     * @param accessUser 用户名称
     * @return 验证码标记
     */
    public boolean answerIsCaptcha(AccessUser accessUser) {
        return actIsCaptcha(accessUser, "answer");
    }


    /**---------------------------------- 用户登录 ---------------------------------------**/
    /**
     * 是否显示验证码
     *
     * @param userName 用户名称
     * @return
     */
    public boolean loginIsCaptcha(String userName) {

        //是否需要验证码  true:要  false:不要
        boolean isCaptcha = false;
        SettingManage settingManage = SpringContextHolder.getBean(SettingManage.class);
        //用户每分钟提交次数
        Integer quantity = settingManage.getSubmitQuantity("login", userName);

        //如果每用户每分钟提交超过设定次数，则需验证码
        if (quantity != null && quantity >= settingManage.findSystemSettingCache().getLoginSubmitQuantity()) {
            isCaptcha = true;
        }

        return isCaptcha;
    }
}
